window.addEvent('domready', function(){
	var form = $('frmSearch');
	
	//pagination
	new Pagination($('search_table_pagination'),{
		onPageChange: function(obj){
			form.offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	//table header untuk sorting
	new SortableTableHeader($('search_table').getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','perb_tempat_kejadian','perb_keterangan','perb_status'],
		onHeaderClick:function(item, fieldName, sort){
			form.orderby.value = fieldName+" "+sort;
			search_cont(form);
		}
	});
	
	search(form);
});

function search(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('search_table_content').set('html', json.table_content);
			
			//rubah jumlah halamannya
			$('search_table_pagination').retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('search_table_content').set('html', json.table_content);
		}
	}).send();
}

function showImage(title, imageTitle, imageUrl){
	FBModal_show2(APP.utils+'/imageViewer', 'post', 
		"title="+title+
		"&imageTitle="+imageTitle+
		"&imageUrl="+imageUrl+
		"&maxWidth="+(screen.width-50)+
		"&maxHeight="+(screen.height-50), true, false, null, {
		onSuccess: function(res){}
		});
}

function redirectToView(perb_notif_id, perb_id){
	ajax(APP.utils+'/notification/updateLook/'+perb_notif_id,'post','',$empty,
		function(res){
			document.location=APP.base+'/persetujuan_detailView/'+perb_id;
		});
}

function doUpdateStatus(id, status){
	ajax(APP.base+'/ajax/updateStatus/'+id+'/'+status, 'post', "", $empty, 
		function(res){
			search($('frmSearch'));
		});
}